package com.fantastic.web.ex;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.result.ResultData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: rxx
 * @since: 2024/5/23 15:44
 * @description: 执行sql语句异常处理器
 */
//标明为全局异常处理类
@ControllerAdvice
//返回响应体，自动json（导一个自动转换的包）
@ResponseBody
public class SqlExceptionAdvice {

    /**
     * 查询时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(SelectException.class)
    public ResultData SelectExceptionMethod(SelectException e) {
        ResultData resultData = new ResultData(e.getResponseStatusEnum());
        return resultData;
    }

    /**
     * 添加时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(InsertException.class)
    public ResultData InsetExceptionMethod(InsertException e) {
        ResultData resultData = new ResultData(e.getResponseStatusEnum());
        return resultData;
    }

    /**
     * 修改时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UpdateException.class)
    public ResultData UpdateExceptionMethod(UpdateException e) {
        return new ResultData(e.getResponseStatusEnum());
    }

    /**
     * 删除时异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(DeleteException.class)
    public ResultData SelectExceptionMethod(DeleteException e) {
        return new ResultData(e.getResponseStatusEnum());
    }


    /**
     * 兜底全局异常，非自定义的异常进入到此捕获
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResultData exception(Exception e) {
        e.printStackTrace();
        //响应服务器错误码
        return new ResultData(RespStatusInfo.SERVER_ERROR);
    }

}
